/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.ErrorLog;
import me.zhengjie.modules.biz.service.ErrorLogService;
import me.zhengjie.modules.biz.service.dto.ErrorLogQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-03-16
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "error-log管理")
@RequestMapping("/api/errorLog")
public class ErrorLogController {

    private final ErrorLogService errorLogService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('errorLog:list')")
    public void exportErrorLog(HttpServletResponse response, ErrorLogQueryCriteria criteria) throws IOException {
        errorLogService.download(errorLogService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询error-log")
    @ApiOperation("查询error-log")
//    @PreAuthorize("@el.check('errorLog:list')")
    public ResponseEntity<Object> queryErrorLog(ErrorLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(errorLogService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增error-log")
    @ApiOperation("新增error-log")
//    @PreAuthorize("@el.check('errorLog:add')")
    public ResponseEntity<Object> createErrorLog(@Validated @RequestBody ErrorLog resources){
        return new ResponseEntity<>(errorLogService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改error-log")
    @ApiOperation("修改error-log")
//    @PreAuthorize("@el.check('errorLog:edit')")
    public ResponseEntity<Object> updateErrorLog(@Validated @RequestBody ErrorLog resources){
        errorLogService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除error-log")
    @ApiOperation("删除error-log")
//    @PreAuthorize("@el.check('errorLog:del')")
    public ResponseEntity<Object> deleteErrorLog(@RequestBody Integer[] ids) {
        errorLogService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping(value = "/currentList")
    @ApiOperation("实时报警列表")
    @AnonymousAccess
    public ResponseEntity<Object> currentList(ErrorLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(errorLogService.currentList(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/historyList")
    @ApiOperation("历史报警列表")
    @AnonymousAccess
    public ResponseEntity<Object> historyList(ErrorLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(errorLogService.historyList(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/floorList")
    @ApiOperation("楼层报警列表")
    @AnonymousAccess
    public ResponseEntity<Object> floorList(ErrorLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(errorLogService.floorList(criteria,pageable),HttpStatus.OK);
    }
}
